package domain;

import java.util.HashMap;
import java.util.Set;

public class ShoppingCart {

	private HashMap<String, ItemOrder> cartitems= new HashMap<String, ItemOrder> ();

	public HashMap<String, ItemOrder> getShoppingCart() {
		return cartitems;
	}

	public void addItem(ItemOrder item) {
		if (getShoppingCart().isEmpty()){
			getShoppingCart().put(item.getItemID(), item);}
		Set<String> keyset = getShoppingCart().keySet();
		
		
		if (item.getQuantity() == 0) {
			deleteItem(item);
		} else {
			
			if (keyset.contains(item.getItemID())) {
				// if the item already exists in the shopping cart we only
				// modify the quantity
				String existingitemid = item.getItemID();
				getShoppingCart().get(existingitemid).setQuantity(
						item.getQuantity());

			} else {
				// we add a new entry to the HashMap
				getShoppingCart().put(item.getItemID(), item);

			}
		}
	}

	public void deleteItem(ItemOrder item) {
		getShoppingCart().remove(item.getItemID());
	}

}
